# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=lua-curl
_pkgname=Lua-cURLv3
pkgver=0.3.13
pkgrel=1
pkgdesc="Lua bindings to cURL library"
url="https://github.com/Lua-cURL/Lua-cURLv3"
arch="all"
license="MIT"
makedepends="curl-dev"
source="$pkgname-$pkgver.tar.gz::https://github.com/Lua-cURL/$_pkgname/archive/v$pkgver.tar.gz"

builddir="$srcdir/$_pkgname-$pkgver"
options="!check"

_luaversions="5.1 5.2 5.3 5.4"
for _v in $_luaversions; do
	makedepends="$makedepends lua$_v-dev"
	subpackages="$subpackages lua$_v-${pkgname#lua-}:_subpackage"
done

prepare() {
	default_prepare

	local lver; for lver in $_luaversions; do
		cp -r "$builddir" "$builddir-$lver"
	done
}

build() {
	local lver; for lver in $_luaversions; do
		msg "Building for Lua $lver..."

		cd "$builddir-$lver"
		echo LUA_IMPL=lua$lver >> .config
		make LUA_VERSION=$lver
	done
}

package() {
	mkdir -p "$pkgdir"
}

_subpackage() {
	local lver="${subpkgname:3:3}"
	pkgdesc="$pkgdesc (for Lua $lver)"
	depends="lua$lver"
	install_if="$pkgname=$pkgver-r$pkgrel lua$lver"
	local rockdir="$subpkgdir/usr/lib/luarocks/rocks-$lver/$pkgname/$pkgver-1"

	cd "$builddir-$lver"
	make install DESTDIR="$subpkgdir" LUA_VERSION=$lver

	mkdir -p "$rockdir"
	echo 'rock_manifest = {}' > "$rockdir"/rock_manifest
}

sha512sums="4283109e349b994e0d2349a45b37a95bab7a7fc115740a4ce078ff967485364714691eac3398db90bacb54584790d6ec778d845fb7ecc7716c0863c454216f05  lua-curl-0.3.13.tar.gz"
